//
//  GYMainViewController.swift
//  Swift-sina
//
//  Created by gaoyanyun on 16/5/10.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit

class GYMainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        addChildViewControllers()
        setupTabbar()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        
        setupComposeButton()
    }
    
       // 设置撰写按钮位置
    private func setupComposeButton() {
        // 前置按钮(把按钮弄到前面)
        tabBar.bringSubviewToFront(composeButton)
        
        // 设置按钮位置  宽度>0 向内缩小 <0向外
        //-1是防止边界处理
        let w = tabBar.bounds.width / CGFloat(childViewControllers.count) - 1
        composeButton.frame = CGRectInset(tabBar.bounds, 2 * w, 0)
    }
    
    
    //设置私有的控件
    //撰写按钮
//    private lazy var composeButton: UIButton = {
//        let btn = UIButton()
//        
//        let imageName = "tabbar_compose_icon_add"
//        btn.setImage(UIImage(named: imageName), forState: .Normal)
//        btn.setImage(UIImage(named: imageName + "_highlighted"), forState: .Highlighted)
//        
//        let backgroundImageName = "tabbar_compose_button"
//        btn.setBackgroundImage(UIImage(named: backgroundImageName), forState: .Normal)
//        btn.setBackgroundImage(UIImage(named: backgroundImageName + "_highlighted"), forState: .Highlighted)
//        
//        btn.sizeToFit()
//        
//        return btn
//    }()
    //撰写按钮
   private lazy var composeButton: UIButton = UIButton(
    
    imageName: "tabbar_compose_icon_add",
    
    backgroundImageName: "tabbar_compose_button"
    )
    
    
    //设置分割线
    private func setupTabbar(){
        
        //取消tabbar分割线
        tabBar.shadowImage = UIImage.cz_singleDotImage(UIColor(white: 0.8, alpha: 1.0))
        
        tabBar.backgroundImage = UIImage(named: "tabbar_background")
        
        
        //添加加号按钮
        tabBar.addSubview(composeButton)
        
        //添加加号按钮的点击方法
        composeButton.addTarget(self, action: #selector(clickComposeButton), forControlEvents: .TouchUpInside)
    }

    //添加监听加号按钮的方法
    @objc private func clickComposeButton(){
        
        print(#function)
        
    }

    // MARK: 添加控制器
    private func addChildViewController(vc: UIViewController, title: String, imageName: String) {
        
        //let vc = GYHomeViewController()
        
        vc.title=title
        vc.tabBarItem.image = UIImage (named: imageName)
        
        let nav = UINavigationController (rootViewController: vc)
        addChildViewController(nav)
        //设置选中的图像
        vc.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")
        
        //设置选中图像的渲染模式
        vc.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.imageWithRenderingMode(.AlwaysOriginal)
        
        //设置标题颜色
        tabBar.tintColor = UIColor.orangeColor()
        
        //设置字体属性
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.orangeColor()], forState: .Selected)
        vc.tabBarItem.setTitleTextAttributes([NSFontAttributeName:UIFont.systemFontOfSize(13)], forState: .Normal)
        
        
        
    }
    
 
    
    private func addChildViewControllers(){
        
        addChildViewController(GYHomeViewController(), title: "首页 ", imageName: "tabbar_home")
        addChildViewController(GYMessageViewController(), title: "消息", imageName: "tabbar_message_center")
        //添加空白控制器
        addChildViewController(UIViewController())
        
        addChildViewController(GYDiscoverViewController(), title: "发现", imageName: "tabbar_discover")
        addChildViewController(GYProfileViewController(), title: "我 ", imageName: "tabbar_profile")
        
    }
    
    
  
}
 
